using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace UserManagementService.Users
{
    /// <summary>
    /// 用户角色关联实体
    /// </summary>
    public class UserRole : CreationAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public virtual Guid UserId { get; protected set; }

        /// <summary>
        /// 角色ID
        /// </summary>
        public virtual Guid RoleId { get; protected set; }

        /// <summary>
        /// 角色名称（冗余字段，便于查询）
        /// </summary>
        public virtual string? RoleName { get; protected set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public virtual bool IsEnabled { get; protected set; }

        /// <summary>
        /// 角色分配时间
        /// </summary>
        public virtual DateTime AssignedAt { get; protected set; }

        /// <summary>
        /// 分配人ID
        /// </summary>
        public virtual Guid? AssignedById { get; protected set; }

        /// <summary>
        /// 过期时间（可为空，表示永不过期）
        /// </summary>
        public virtual DateTime? ExpiresAt { get; protected set; }

        /// <summary>
        /// 备注
        /// </summary>
        public virtual string? Remark { get; protected set; }

        protected UserRole()
        {
            IsEnabled = true;
            AssignedAt = Clock.Now;
        }

        /// <summary>
        /// 创建用户角色关联
        /// </summary>
        public UserRole(
            Guid id,
            Guid userId,
            Guid roleId,
            string? roleName = null,
            Guid? assignedById = null,
            DateTime? expiresAt = null,
            string? remark = null) : base(id)
        {
            UserId = userId;
            RoleId = roleId;
            RoleName = roleName;
            AssignedById = assignedById;
            ExpiresAt = expiresAt;
            Remark = Check.Length(remark, nameof(remark), maxLength: 500);

            IsEnabled = true;
            AssignedAt = Clock.Now;
        }

        /// <summary>
        /// 启用角色
        /// </summary>
        public void Enable()
        {
            IsEnabled = true;
        }

        /// <summary>
        /// 禁用角色
        /// </summary>
        public void Disable()
        {
            IsEnabled = false;
        }

        /// <summary>
        /// 更新角色名称
        /// </summary>
        public void UpdateRoleName(string roleName)
        {
            RoleName = Check.Length(roleName, nameof(roleName), maxLength: 256);
        }

        /// <summary>
        /// 更新过期时间
        /// </summary>
        public void UpdateExpiresAt(DateTime? expiresAt)
        {
            ExpiresAt = expiresAt;
        }

        /// <summary>
        /// 检查角色是否已过期
        /// </summary>
        public bool IsExpired()
        {
            return ExpiresAt.HasValue && ExpiresAt.Value < Clock.Now;
        }

        /// <summary>
        /// 检查角色是否有效
        /// </summary>
        public bool IsValid()
        {
            return IsEnabled && !IsExpired();
        }
    }
}